package borge.web.teamspirit.client.page.team.widgets;

import java.util.List;

import borge.web.teamspirit.client.SessionData;
import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.TeamSpiritConstants;
import borge.web.teamspirit.client.data.Team;
import borge.web.teamspirit.client.interfaces.PersistentData;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.PersistentData.DataKey;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;
import borge.web.teamspirit.client.page.shared.widgets.ListDataWidget;
import borge.web.teamspirit.client.servercomm.DBQuery;
import borge.web.teamspirit.client.servercomm.ServerComm;

public class ListTeamsWidget extends ListDataWidget {

	private String iTitle;
	
	// Either title as string, or take Association/LeagueDivision..but we might
	// want to list teams in many different ways..so ..might be best with string and handle
	// context outside of this widget
	public ListTeamsWidget(WidgetEventHandler aEventHandler, String aTitle, List<PersistentData> aTeams){

		super(aEventHandler, aTeams, DataKey.TEAM, WidgetEventType.EDIT_TEAM, 
				WidgetEventType.DELETE_TEAM, WidgetEventType.SHOW_TEAM);
		iTitle = aTitle;
		// populate table with series information
		
		//iPanel.add(new Label("test"));
		
		if(iData != null)
			createDataTable();
		
		initWidget(iPanel);
		
	}
	
	@Override
	protected void createDataTable() {
		// TODO Auto-generated method stub
		
		SessionData.sTeamsList = iData;
		
		reset();
		
        TeamSpiritConstants lcs = TeamSpirit.getConstants();
        int row = 0;
        int col = 0;
        
        //if(iTitle != ull)
        	//iTable.setText(row++, col, iTitle);
        
        for(PersistentData pd : iData){
        	
        	Team team = (Team)pd;
        	
        	iTable.setText(row, col++, team.getName());
        	addButtons(row++, col);
        	col = 0;
        }
        
        //iPanel.add(iTable);
		
	}

	@Override
	public void requestServerData(ServerCallbackHandler aHandler) {
		ServerComm.getObjectList(new DBQuery(DataKey.TEAM, Team.class.getName()), aHandler);
	}
	
}
